package com.yanqu.road.dao.impl.activity.langyabang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.langyabang.data.LangYaUserCaseGameData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class LangYaBangUserCaseDataDaoImpl extends TempDataObjectDao<LangYaUserCaseGameData> {
    public static String getLangYaUserCaseGameDataSql = "select * from `t_u_langya_user_case_data` where `activityId`=? and `userId`=?";
    public LangYaUserCaseGameData getLangYaUserCaseGameData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getLangYaUserCaseGameDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateLangYaUserCaseGameDataSql = "update `t_u_langya_user_case_data` set allCaseData=?,caseGameStatus=?,caseGameNowCase=?,caseGameNowCard=?,caseGameNowValue=?,hasReadTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(LangYaUserCaseGameData langYaUserCaseGameData) {
        langYaUserCaseGameData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();

        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaUserCaseGameData.getAllCaseData())));
        para.put(i++, new DbParameter(Types.INTEGER, langYaUserCaseGameData.getCaseGameStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaUserCaseGameData.getCaseGameNowCase()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaUserCaseGameData.getCaseGameNowCard()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaUserCaseGameData.getCaseGameNowValue())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaUserCaseGameData.getHasReadTime()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaUserCaseGameData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaUserCaseGameData.getUserId()));
        if (getDb().executeNoneQuery(updateLangYaUserCaseGameDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertLangYaUserCaseGameDataSql = "insert into `t_u_langya_user_case_data`(activityId,userId,allCaseData,caseGameStatus,caseGameNowCase,caseGameNowCard,caseGameNowValue,hasReadTime) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(LangYaUserCaseGameData langYaUserCaseGameData) {
        langYaUserCaseGameData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, langYaUserCaseGameData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaUserCaseGameData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaUserCaseGameData.getAllCaseData())));
        para.put(i++, new DbParameter(Types.INTEGER, langYaUserCaseGameData.getCaseGameStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaUserCaseGameData.getCaseGameNowCase()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaUserCaseGameData.getCaseGameNowCard()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaUserCaseGameData.getCaseGameNowValue())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaUserCaseGameData.getHasReadTime()));
        if (getDb().executeNoneQuery(insertLangYaUserCaseGameDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected LangYaUserCaseGameData parseData(ResultSet rs) throws SQLException {
        LangYaUserCaseGameData langYaUserCaseGameData = new LangYaUserCaseGameData();
        langYaUserCaseGameData.setActivityId(rs.getInt("activityId"));
        langYaUserCaseGameData.setUserId(rs.getLong("userId"));
        langYaUserCaseGameData.setAllCaseData(JSON.parseObject(rs.getString("allCaseData"), new TypeReference<Map<Integer, Integer>>() {}));
        langYaUserCaseGameData.setCaseGameStatus(rs.getInt("caseGameStatus"));
        langYaUserCaseGameData.setCaseGameNowCase(rs.getInt("caseGameNowCase"));
        langYaUserCaseGameData.setCaseGameNowCard(rs.getInt("caseGameNowCard"));
        langYaUserCaseGameData.setCaseGameNowValue(JSON.parseObject(rs.getString("caseGameNowValue"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        langYaUserCaseGameData.setHasReadTime(rs.getLong("hasReadTime"));

        return langYaUserCaseGameData;
    }
}
